/* PROFILE */
define(function() {
    
    return {
        go: function(profile) {
            this.profile = profile;
            Template('profile', profile, function(html) {
                $content.html(html);
            });
            this.listen();
            this.getFeed();
        },
        listen: function() {
            var self = this;
            $content.on('focus', '.wall_post textarea', function() {
                $('.wall_post button').show();
            })
            .on('submit', '.wall_post', function(e) {
                e.preventDefault();
                FB.api(self.profile.id + '/feed', 'POST', {message: $('.wall_post textarea').val()}, function(res) {
                    if(res && res.id) {
                        window.location.hash = res.id;
                    } else {
                        require(['alert'], function(alert) {
                            alert({text: 'Could not post to this wall'});
                        });
                    }
                });
                $('.wall_post textarea').val('');
                $('.wall_post button').hide();
            });
        },
        getFeed: function() {
            FB.api(this.profile.id + '/feed', function(res) {
                if(res && res.data && res.data.length && !res.error) {
                    res.data = postParser(res.data);
                    Template('posts', {posts: res.data}, function(html) {
                        $content.append(html);
                        require(['posts'], function(posts) {
                            posts.listen();
                        });
                    }, ['likes','comments']);
                } else {
                    $error("This person's feed is not visible to you", true);
                }
            });
        }
    }
});